from typing import List


class Solution:
    def sumOfBeauties(self, nums: List[int]) -> int:
        size = len(nums)
        prefix = [0] * size  # i左边的最大值
        suffix = [100001] * size  # i右边的最小值
        for i in range(1, size):
            prefix[i] = max(prefix[i - 1], nums[i - 1])
        for i in range(size - 2, -1, -1):
            suffix[i] = min(suffix[i + 1], nums[i + 1])

        ans = 0
        for i in range(1, size - 1):
            if prefix[i] < nums[i] < suffix[i]:
                ans += 2
            elif nums[i - 1] < nums[i] < nums[i + 1]:
                ans += 1
        return ans


if __name__ == "__main__":
    print(Solution().sumOfBeauties(nums=[1, 2, 3]))  # 2
    print(Solution().sumOfBeauties(nums=[2, 4, 6, 4]))  # 1
    print(Solution().sumOfBeauties(nums=[3, 2, 1]))  # 0
